/*
    编写一个函数escape(s, t),将字符串t复制到字符串s中,并在复制过程中将
    换行符,制表符等不可兼得字符分别转换为\n, \t等相应的可见的转义字符序列.
    要求使用switch语句.在编写一个具有相反功能的函数,在复制过程中将转义字符序列转换为实际字符.
*/
#include <stdio.h>

void escape(char* s, char* t);
void unescape(char* s, char* t);

int main(void)
{
    char s[13] ={0,};
    char ss[13] ={0,};
    char t[] = "Hello\n\tworld";

    printf("%s\n", t);
    escape(s, t);
    printf("%s\n", s);
    unescape(ss, s);
    printf("%s\n", ss);

    return 0;
}

void escape(char s[], char t[])
{
    int i = 0;
    int j = 0;

    for (i = 0; t[i] != '\0'; ++i)
    {
        switch (t[i])
        {
            case '\n':
                s[j++] = '\\';
                s[j++] = 'n';
                break;
            case '\t':
                s[j++] = '\\';
                s[j++] = 't';
                break;
            default:
                s[j++] = t[i];
                break;
        }
    }
    s[j] = '\0';
}

void unescape(char s[], char t[])
{
    int i = 0;
    int j = 0;

    for (i = 0; t[i] != '\0'; ++i)
    {
        if (t[i] == '\\')
        {
            ++i;
        }
        switch (t[i])
        {
            case 'n':
                s[j++] = '\n';
                break;
            case 't':
                s[j++] = '\t';
                break;
            default:
                s[j++] = t[i];
                break;
        }
    }
    s[j] = '\0';
}